package Demo.utils;

import Demo.mapper.StudentMapper;
import Demo.model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentUtils {

    private static SqlSession sqlSession = null;
    private static InputStream is = null;
    private static StudentMapper studentMapper = null;

    public static void Config()
    {
        try {
            is = Resources.getResourceAsStream("SqlMapConﬁg.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(is);
            sqlSession = sqlSessionFactory.openSession();
            studentMapper = sqlSession.getMapper(StudentMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void insert(Student student)
    {
        try
        {
            studentMapper.getInsert(student);
            sqlSession.commit();
            System.out.println("数据添加成功.");
        }catch (Exception e)
        {
            System.out.println("该数据已经存在或者数据有误");
        }
    }

    public static int getCountById(String username)
    {
        return studentMapper.getCountByNname(username);
    }

    public static void delete(String username)
    {
        try
        {
            int count = getCountById(username);
            if(count==0)
            {
                int a = 10/0;
            }

            studentMapper.getDelete(username);
            sqlSession.commit();
            System.out.println("删除成功.");
        }catch (Exception e)
        {
            System.out.println("删除错误，该数据不存在");
        }
    }

    public static void update(Student student)
    {
        try
        {
            int count = getCountById(student.getUsername());
            if(count==0)
            {
                int a = 10/0;
            }

            Student student1 = get(student.getUsername());
            if(student.equals(student1))
            {
                System.out.println("新数据和原来的数据一样，请重新输入数据.");
                return;
            }

            studentMapper.getUpdate(student);
            sqlSession.commit();
            System.out.println("更新成功.");
        }catch (Exception e)
        {
            System.out.println("更新出错.");
        }
    }

    public static Student get(String username)
    {
        try
        {
            Student student = studentMapper.getStudent(username);
            if(student==null)
            {
                int a = 10/0;
            }
            return student;
        }catch (Exception e)
        {
            return null;
        }
    }

    public static List<Student> list()
    {
        try
        {
            List<Student> list = studentMapper.findAll();
            if(list==null)
            {
                int a = 10/0;
            }
            return list;
        }catch (Exception e)
        {
            return null;
        }
    }

    public static int getCountByUsername(String username){
        return studentMapper.getCountByNname(username);
    }

}

